package dhc;

import static org.junit.Assert.assertTrue;
import dhc.test.RobotTestBed;
import robocode.control.events.RoundEndedEvent;
import robocode.control.events.TurnEndedEvent;
import robocode.control.snapshot.IRobotSnapshot;
import net.sf.robocode.io.Logger;

/**
 * Tests if the robot's turn fucntion is working correctly. 
 * 
 * @author Daniel Colton
 * 
 */
public class TestMistaRobotoUnit2 extends RobotTestBed {

  /** to count how many times the robot hit the walls. */
  public Integer turnCount = 0;
  /** to indicate that the robot is turning. */
  public boolean robotTurn = false;
  /** heading of the robot one turn ago.  */
  public double headingBefore = -1;
  /** Setting this value to true would indicate that the test has succeeded. */
  public boolean testSuccess = false;

  /**
   * Check for results, output the value. 
   * 
   * @param event is an instance of RoundEndedEvent
   */
  @Override
  public void onRoundEnded(RoundEndedEvent event) {

    Logger.realOut.print(testSuccess);
    assertTrue("Check for Results: ", testSuccess);
    turnCount = 0;
    headingBefore = -1;
  }

  /**
   * Checks to see if the robot is currently turning. It gives
   * 'true' to testSuccess variable. 
   * 
   * @param event is an instance of TurnEndedEvent.
   */
  @Override
  public void onTurnEnded(TurnEndedEvent event) {

    IRobotSnapshot robot = event.getTurnSnapshot().getRobots()[1];
    double currentHeading = Math.toDegrees(robot.getBodyHeading());

    if (headingBefore < 0) {
      headingBefore = currentHeading;
    }
    else {
      if ((((int)(headingBefore - 10) % 360) == (int)currentHeading)
          || (((int)(headingBefore + 10) % 360) == (int)currentHeading)) {
        turnCount++;
      }
      else if (turnCount == 9) {
        testSuccess = true;
      }
      headingBefore = currentHeading;
    }

  }

  /**
   * Specifies that SittingDuck and MistaRoboto are to be matched up in this test case.
   * 
   * @return The comma-delimited list of robots in this match.
   */
  @Override
  public String getRobotNames() {
    return "sample.Tracker,dhc.MistaRoboto";
  }

  /**
   * This test runs for 10 rounds.
   * 
   * @return The number of rounds.
   */
  @Override
  public int getNumRounds() {
    return 5;
  }
}
